package com.printeverywhere.util;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionInvocation;
import com.printeverywhere.dto.Admin;
import com.printeverywhere.dto.AdminModules;

public class SecurityManager {

	public static Boolean checkCredentials(ActionInvocation invocation,Admin admin){
		if(admin==null || admin.getAdminModuleses()==null ||admin.getAdminModuleses().size()<1)return false;
		boolean status=false;
		String thisclass =invocation.getProxy().getAction().getClass().getSimpleName();
		if( exceptionTrue(thisclass,admin))return true;
		else{
			Iterator<AdminModules> it = admin.getAdminModuleses().iterator();
			while(it.hasNext()){
				AdminModules module=it.next();
				status = StringUtils.containsIgnoreCase(module.getModule().getModuleName(),thisclass);
				if(status==true)break;
			}
		}
		return status;
	}




	private static boolean exceptionTrue(String thisclass ,Admin admin ){
		if(thisclass.equalsIgnoreCase("AdminMainMenu") || thisclass.equalsIgnoreCase("AdminLoginAction") || thisclass.equalsIgnoreCase("CredentialExceptionAction")){return true;}
		/*else if(thisclass.equalsIgnoreCase("RegistrationAction")){
			Iterator<AdminModules> it = admin.getAdminModuleses().iterator();
			while(it.hasNext()){
				AdminModules module=it.next();
				if(module.getModule().getModuleName().equalsIgnoreCase("AdminEngineManagement"))return true;
			}
			return true;
		}*/
		else return false;
	}

}
